﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_2._2.Models;
using Project_2._2.ViewModels;
using System.IO;
using Project_2._2.Extra;
using System.Web.Security;
using PagedList;
using PagedList.Mvc;
namespace Project_2._2.Controllers
{
    public class BookCarController : Controller
    {
        private QuanLyThueXeEntities db = new QuanLyThueXeEntities();
        private Common cm = new Common();
        public ActionResult Default(int? page, string rd, string Record)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    if (rd != null)
                    {
                        int record = int.Parse(rd);
                        var qc = (from p in db.DatXes where p.isDelete == false select p).OrderBy(m => m.Duyet).ToList().ToPagedList(page ?? 10, record);
                        ViewBag.record = rd;
                        return View(qc);
                    }
                    else
                    {
                        var qc = (from p in db.DatXes where p.isDelete == false select p).OrderBy(m => m.Duyet).ToList().ToPagedList(page ?? 1, 10);
                        return View(qc);
                    }
                }
                else
                    return RedirectToAction("Login", "Admin");
            }
        }
        [HttpPost]
        public ActionResult Default(FormCollection col, int? page)
        {
            int record = 0;
            if (string.IsNullOrEmpty(col["KeyWord"]))
            {
                record = int.Parse(col["Record"]);
                var bookcar = (from p in db.DatXes where p.isDelete == false select p).OrderBy(m => m.Duyet).ToList().ToPagedList(page ?? 1, record);
                ViewBag.record = col["Record"];
                return View(bookcar);
            }
            else
            {
                string SearchBy = col["SearchBy"].ToString();
                string key = col["KeyWord"].ToString();

                switch (SearchBy)
                {
                    case "HoTen":
                        var bookcar = (from p in db.DatXes where p.isDelete == false & p.HoTen.Contains(key) select p).OrderBy(m => m.Duyet).ToList().ToPagedList(page ?? 1, 10);
                        ViewBag.record = "1";
                        return View(bookcar); 
                        break;
                    case "Email":
                        bookcar = (from p in db.DatXes where p.isDelete == false & p.Email.Contains(key) select p).OrderBy(m => m.Duyet).ToList().ToPagedList(page ?? 1, 10);
                        ViewBag.record = "1";
                        return View(bookcar); 
                        break;
                    default:
                        break;
                }
                return View();
            }
        }

        [HttpPost]
        public ActionResult Multi_Delete(IEnumerable<int> DatXeIdsToDelete)
        {
            var Bcar_delete = db.DatXes.Where(x => DatXeIdsToDelete.Contains(x.DatXeId)).ToList();
            foreach (var Bcar in Bcar_delete)
            {
                Bcar.isDelete = true;
                UpdateModel(Bcar);
                db.SaveChanges();
            }
            return RedirectToAction("Default", "BookCar");
        }

        public ActionResult Accept(int id)
        {
            DatXe Bcar = (from p in db.DatXes where p.DatXeId == id select p).First();
            Bcar.Duyet = true;
            UpdateModel(Bcar);
            db.SaveChanges();
            return RedirectToAction("Default", "BookCar");
        }

        public ActionResult Delete(int id)
        {
            DatXe Bcar = (from p in db.DatXes where p.DatXeId == id select p).First();
            Bcar.isDelete = true;
            UpdateModel(Bcar);
            db.SaveChanges();
            return RedirectToAction("Default", "bookcar");
        }

        public ActionResult Search()
        {
            return View();
        }
    }
}
